package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.DataScope;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.ZzCustomer;
import com.zzedu.dto.ZzCustomerDTO;
import com.zzedu.mapper.ZzCustomerMapper;
import com.zzedu.service.IZzCustomerService;
import com.zzedu.system.service.ISysRegionService;
import com.zzedu.vo.ZzCustomerVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 销售信息Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-05-29
 */
@Service
public class ZzCustomerServiceImpl extends ServiceImpl<ZzCustomerMapper, ZzCustomer> implements IZzCustomerService {
    @Autowired
    ISysRegionService sysRegionService;

    @Override
    public LambdaQueryWrapper<ZzCustomer> buildQuery(ZzCustomerDTO zzCustomer) {
        LambdaQueryWrapper<ZzCustomer> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzCustomer.getCustName()), ZzCustomer::getCustName, zzCustomer.getCustName());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzCustomer.getLinkName()), ZzCustomer::getLinkName, zzCustomer.getLinkName());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzCustomer.getLinkTel()), ZzCustomer::getLinkTel, zzCustomer.getLinkTel());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzCustomer.getProductName()), ZzCustomer::getProductName, zzCustomer.getProductName());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzCustomer.getLevel()), ZzCustomer::getLevel, zzCustomer.getLevel());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzCustomer.getSourceChannel()), ZzCustomer::getSourceChannel, zzCustomer.getSourceChannel());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzCustomer.getSignState()), ZzCustomer::getSignState, zzCustomer.getSignState());
        lambdaQueryWrapper.eq(null != zzCustomer.getIsAllPay(), ZzCustomer::getIsAllPay, zzCustomer.getIsAllPay());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzCustomer.getNextDoUserName()), ZzCustomer::getNextDoUserName, zzCustomer.getNextDoUserName());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzCustomer.getMark()), ZzCustomer::getMark, zzCustomer.getMark());
        List<String[]> queryProvinceAndCityAndDistrict = zzCustomer.getQueryProvinceAndCityAndDistrict();
        if (CollectionUtils.isNotEmpty(queryProvinceAndCityAndDistrict)) {
            List<String> province = Lists.newArrayList();
            List<String> city = Lists.newArrayList();
            List<String> district = Lists.newArrayList();
            for (String[] provinceAndCityAndDistrict : queryProvinceAndCityAndDistrict) {
                if (null == provinceAndCityAndDistrict) {
                    continue;
                }
                if (StringUtils.isNotBlank(provinceAndCityAndDistrict[0])) {
                    province.add(provinceAndCityAndDistrict[0]);
                }
                if (provinceAndCityAndDistrict.length > 1 && StringUtils.isNotBlank(provinceAndCityAndDistrict[1])) {
                    city.add(provinceAndCityAndDistrict[1]);
                }
                if (provinceAndCityAndDistrict.length > 2 && StringUtils.isNotBlank(provinceAndCityAndDistrict[2])) {
                    district.add(provinceAndCityAndDistrict[2]);
                }
            }
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(province), ZzCustomer::getProvince, province);
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(city), ZzCustomer::getCity, city);
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(district), ZzCustomer::getDistrict, district);
        }
        lambdaQueryWrapper.orderByDesc(ZzCustomer::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public List<ZzCustomerVO> convertVO(List<ZzCustomer> records) {
        return Optional.ofNullable(records).orElse(Lists.newArrayList()).stream().map(zzOrganization -> {
            ZzCustomerVO zzOrganizationVO = new ZzCustomerVO();
            BeanUtils.copyBeanProp(zzOrganizationVO, zzOrganization);
            List<String> provinceAndCityAndDistrict = sysRegionService.provinceAndCityAndDistrict(Lists.newArrayList(zzOrganization.getProvince(), zzOrganization.getCity(), zzOrganization.getDistrict()));
            if (CollectionUtils.isNotEmpty(provinceAndCityAndDistrict)) {
                zzOrganizationVO.setProvinceAndCityAndDistrict(Joiner.on("/").join(provinceAndCityAndDistrict));
            }
            return zzOrganizationVO;
        }).collect(Collectors.toList());
    }

    @Override
    @DataScope
    public <E extends IPage<ZzCustomer>> E page(E page, Wrapper<ZzCustomer> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    @DataScope
    public List<ZzCustomerVO> statistic() {
        QueryWrapper queryWrapper = new QueryWrapper(ZzCustomerVO.class);
        queryWrapper.select("sign_state as signState, count(*) as count");
        queryWrapper.groupBy("sign_state");
        List<Map<String, Object>> result = this.baseMapper.selectMaps(queryWrapper);
        List<ZzCustomerVO> zzCustomerVOList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(result)) {
            Integer count = 0;
            for (Map map : result) {
                ZzCustomerVO zzCustomerVO = new ZzCustomerVO();
                zzCustomerVO.setSignState(MapUtils.getString(map, "signState"));
                zzCustomerVO.setCount(MapUtils.getInteger(map, "count"));
                count += zzCustomerVO.getCount();
                zzCustomerVOList.add(zzCustomerVO);
            }
            ZzCustomerVO zzCustomerVO = new ZzCustomerVO();
            zzCustomerVO.setSignState("合计");
            zzCustomerVO.setCount(count);
            zzCustomerVOList.add(zzCustomerVO);
        }
        return zzCustomerVOList;
    }
}